/*
 * Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved.
 *
 * https://www.mall4j.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */
package com.yami.shop.service.impl;


import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.BooleanUtil;
import cn.hutool.core.util.PhoneUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.yami.shop.bean.app.dto.StationSalesDto;
import com.yami.shop.bean.enums.OfflineHandleEventStatus;
import com.yami.shop.bean.enums.OfflineHandleEventType;
import com.yami.shop.bean.enums.StationEnum;
import com.yami.shop.bean.enums.StationStatusEnum;
import com.yami.shop.bean.model.OfflineHandleEvent;
import com.yami.shop.bean.model.Station;
import com.yami.shop.bean.param.OfflineHandleEventAuditParam;
import com.yami.shop.bean.param.StationDetailParam;
import com.yami.shop.bean.param.TimeParam;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.common.util.Arith;
import com.yami.shop.common.util.Json;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.common.util.PrincipalUtil;
import com.yami.shop.dao.StationMapper;
import com.yami.shop.service.OfflineHandleEventService;
import com.yami.shop.service.StationService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 自提点
 *
 * @author YXF
 * @date 2020-04-23 15:18:29
 */
@Service
@RequiredArgsConstructor
public class StationServiceImpl extends ServiceImpl<StationMapper, Station> implements StationService {

    @Value("${yami.expose.operation.auth:}")
    private Boolean permission;

    private final StationMapper stationMapper;

    private final OfflineHandleEventService offlineHandleEventService;
    private final int TEN = 10;
    private final int TWELVE = 12;
    private final int EIGHTEEN = 18;
    private final int THIRTY = 30;

    @Override
    public IPage<Station> getPage(PageParam<Station> page, Station station) {
        IPage<Station> mapperPage = stationMapper.getPage(page, station);
        if (BooleanUtil.isFalse(permission)){
            for (Station record : mapperPage.getRecords()) {
                if (PrincipalUtil.isMobile(record.getPhone())){
                    record.setPhone(PhoneUtil.hideBetween(record.getPhone()).toString());
                }
            }
        }
        return mapperPage;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void auditApply(Long eventId, Long handleId, String reapplyReason) {
        // 更新活动为待审核状态
        stationMapper.updateStatus(handleId, StationEnum.WAIT_AUDIT.getValue());

        // 更新事件状态
        offlineHandleEventService.updateToApply(eventId, reapplyReason);
    }

    @Override
    public IPage<StationDetailParam> getStationList(Double lat, Double lng, String stationName, Long shopId, String userId, Page page) {
        IPage<StationDetailParam> stationPage = stationMapper.getStationList(page, lat, lng,stationName, shopId,null);
        List<StationDetailParam> stationList = stationPage.getRecords();
        //获取常用的自提点
        StationDetailParam defaultStation = stationMapper.selectGoOften(userId,shopId);
        if(defaultStation != null) {
            IPage<StationDetailParam> stationDetailParamIpage = stationMapper.getStationList(page,lat, lng,"", shopId,defaultStation.getStationId());
            List<StationDetailParam> stationParams = stationDetailParamIpage.getRecords();
            StationDetailParam stationParam = stationParams.get(0);
            stationParam.setDefaultStation(1);
            stationList.add(stationParam);
        }
        for (StationDetailParam stationParam : stationList){
            double distance = stationParam.getDistance() ;
            stationParam.setDistance(distance);
            String timeDate = stationParam.getTimeDate();
            StationDetailParam.TimeDateModeVO timeDateModeVO = Json.parseObject(timeDate, StationDetailParam.TimeDateModeVO.class);
            List<TimeParam> timeParams = new ArrayList<>();
            Long[] shopTime = timeDateModeVO.getShopTime();
            int startHour = DateUtil.hour(new Date(shopTime[0]),true);
            int endHour = DateUtil.hour(new Date(shopTime[1]),true);
            String startTime = DateUtil.format(new Date(shopTime[0]),"HH:mm");
            String endTime = DateUtil.format(new Date(shopTime[1]),"HH:mm");
            List<String> hourTimes = Lists.newArrayList();
            Date startStationTime = DateUtil.offsetDay(new Date(), Integer.valueOf(timeDateModeVO.getStationStartTime()));
            Date endStationTime = DateUtil.offsetDay(startStationTime,Integer.valueOf(timeDateModeVO.getStationEndTime()));
            String currentTime = DateUtil.format(new Date(),"HH:mm");
            int currentHour = DateUtil.hour(startStationTime,true);
            int currentMinute = DateUtil.minute(startStationTime);
            //如果营业时间点大于当前时间点或提货日期不是今天，当前时间改为营业时间
            try{
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                long startDay = sdf.parse(sdf.format(startStationTime)).getTime();
                long currentDay = sdf.parse(sdf.format(new Date())).getTime();
                if(startHour > currentHour || startDay > currentDay ) {
                    currentHour = startHour;
                    currentMinute = DateUtil.minute(new Date(shopTime[0]));
                }else if(startHour == currentHour){
                    //如果相等，比较分钟
                    if(DateUtil.minute(new Date(shopTime[0])) > currentMinute){
                        currentMinute = DateUtil.minute(new Date(shopTime[0]));
                    }
                }
            }catch (ParseException e){
                e.printStackTrace();
            }
            if (Integer.valueOf(timeDateModeVO.getStationStartTime()).equals(0) && endHour < currentHour){
                startStationTime = DateUtil.offsetDay(startStationTime, 1);
            }else {
                if (timeDateModeVO.getInterval().equals(2) && Integer.valueOf(timeDateModeVO.getStationStartTime()).equals(0) && endHour > currentHour){
                    hourTimes = getBusinessByNoon(currentHour, endHour, currentTime, endTime);
                    timeParams.add(new TimeParam(DateUtil.format(startStationTime,"MM-dd"),hourTimes));
                    startStationTime = DateUtil.offsetDay(startStationTime,1);
                }else if (timeDateModeVO.getInterval().equals(3)){
                    hourTimes = getBusinessByHour(currentHour, endHour, currentMinute, DateUtil.minute(new Date(shopTime[1])));
                    timeParams.add(new TimeParam(DateUtil.format(startStationTime,"MM-dd"),hourTimes));
                    startStationTime = DateUtil.offsetDay(startStationTime,1);
                }else if (timeDateModeVO.getInterval().equals(4)){
                    hourTimes = getBusinessByHalfHour(currentHour, endHour, currentMinute, DateUtil.minute(new Date(shopTime[1])));
                    timeParams.add(new TimeParam(DateUtil.format(startStationTime,"MM-dd"),hourTimes));
                    startStationTime = DateUtil.offsetDay(startStationTime,1);
                }
            }
            while (startStationTime.getTime() <= endStationTime.getTime()){
                if (timeDateModeVO.getInterval().equals(1)){
                    List<String> hourTimeList = Lists.newArrayList();
                    //hourTimeList.add(startHour + ":00-" + endHour + ":00");
                    hourTimeList.add(startTime +"-" + endTime);
                    timeParams.add(new TimeParam(DateUtil.format(startStationTime,"MM-dd"),hourTimeList));
                }else if (timeDateModeVO.getInterval().equals(2)){
                    hourTimes = getBusinessByNoon(startHour, endHour, startTime, endTime);
                    timeParams.add(new TimeParam(DateUtil.format(startStationTime,"MM-dd"),hourTimes));
                }else if (timeDateModeVO.getInterval().equals(3)){
                    hourTimes = getBusinessByHour(startHour, endHour, DateUtil.minute(new Date(shopTime[0])), DateUtil.minute(new Date(shopTime[1])));
                    timeParams.add(new TimeParam(DateUtil.format(startStationTime,"MM-dd"),hourTimes));
                }else if (timeDateModeVO.getInterval().equals(4)){
                    hourTimes = getBusinessByHalfHour(startHour, endHour, DateUtil.minute(new Date(shopTime[0])), DateUtil.minute(new Date(shopTime[1])));
                    timeParams.add(new TimeParam(DateUtil.format(startStationTime,"MM-dd"),hourTimes));
                }
                startStationTime = DateUtil.offsetDay(startStationTime,1);
            }
            stationParam.setTimeParams(timeParams);
            stationParam.setBusiness(startTime+ "-"+ endTime);
        }
        return stationPage;
    }

    /**
     * 获取上中下午取货时间列表
     * @param startHour  开始时间（小时）
     * @param endHour   结束时间（小时）
     * @param startTime 营业开始时间
     * @param endTime 营业结束时间
     * @return
     */
    private List<String> getBusinessByNoon(int startHour, int endHour,String startTime,String endTime) {
        String morning = "早上";
        String noon = "下午";
        String night = "晚上";
        List<String> hourTime = Lists.newArrayList();
        if (startHour < TWELVE){
            if (endHour > EIGHTEEN){
                hourTime.add(morning + "(" + startTime + "-12:00)");
                hourTime.add(noon + "(12:00-18:00)");
                hourTime.add(night + "(18:00-" + endTime + ")");
            }else if(endHour == EIGHTEEN){
                hourTime.add(morning + "(" + startTime + "-12:00)");
                hourTime.add(noon + "(12:00-18:00)");
                String minute = endTime.split(":")[1];
                if(!minute.equals("00")){
                    hourTime.add(night + "(18:00-" + endTime + ")");
                }
            }else if (endHour > TWELVE){
                hourTime.add(morning + "(" + startTime + "-12:00)");
                hourTime.add(noon + "(12:00-" + endTime+ ")");
            }else {
                hourTime.add(morning + "(" + startTime + "-" + endTime + ")");
            }
        }else if (startHour >= TWELVE && startHour < EIGHTEEN){
            if (endHour > EIGHTEEN){
                hourTime.add(noon + "(" + startTime + "-18:00)");
                hourTime.add(night + "(18:00-" + endTime + ")");
            }else if(endHour == EIGHTEEN){
                hourTime.add(noon + "(" + startTime + "-18:00)");
                String minute = endTime.split(":")[1];
                if(!minute.equals("00")){
                    hourTime.add(night + "(18:00-" + endTime + ")");
                }
            }else {
                hourTime.add(night + "(" + startTime + "-" + endTime + ")");
            }
        }else if(startHour >= EIGHTEEN){
            hourTime.add(night + "(" + startTime + "-" + endTime + ")");
        }
        return hourTime;
    }

    private List<String> getBusinessByHour(int startHour, int endHour, int startMinute, int endMinute) {
            String startMinuteStr = "";
        String endMinuteStr = "";
        if (startMinute < TEN){
            startMinuteStr = "0" + startMinute;
        }else {
            startMinuteStr = startMinuteStr + startMinute;
        }
        if (endMinute < TEN){
            endMinuteStr = "0" + endMinute;
        }else {
            endMinuteStr = endMinuteStr + endMinute;
        }

        List<String> hourTime = Lists.newArrayList();
        int hour = startHour;
        if (startHour == endHour){
            hourTime.add(startHour + ":" + startMinuteStr + "-" + endHour + ":"+ endMinuteStr);
        }
        if (startMinute > 0){
            hourTime.add(startHour + ":" + startMinuteStr + "-" + ++hour + ":00");
        }
        while (hour < endHour){
            hourTime.add(hour + ":00-" + ++hour + ":00");
        }
        if (endMinute > 0){
            hourTime.add(hour + ":00-" + hour + ":" + endMinuteStr);
        }
        return hourTime;
    }

    private List<String> getBusinessByHalfHour(int startHour, int endHour, int startMinute, int endMinute) {
        String startMinuteStr = "";
        String endMinuteStr = "";
        if (startMinute < TEN){
            startMinuteStr = "0" + startMinute;
        }else {
            startMinuteStr = startMinuteStr + startMinute;
        }
        if (endMinute < TEN){
            endMinuteStr = "0" + endMinute;
        }else {
            endMinuteStr = endMinuteStr + endMinute;
        }

        List<String> hourTime = Lists.newArrayList();
        int hour = startHour;
        if (startHour == endHour && startMinute < endMinute){
            if((startMinute >= THIRTY) || (startMinute < THIRTY && endMinute <= THIRTY)){
                hourTime.add(startHour + ":" + startMinuteStr + "-" + endHour + ":" + endMinuteStr );
            }else {
                hourTime.add(startHour + ":" + startMinuteStr + "-" + endHour + ":30" );
                hourTime.add(startHour + ":30" + "-" + endHour + ":" + endMinute );
            }
            return hourTime;
        }
        if (startMinute > 0 && startMinute < THIRTY){
            hourTime.add(startHour + ":" + startMinuteStr + "-" + hour + ":30");
            hourTime.add(startHour + ":30-" + ++hour + ":00");
        }else if (startMinute >= THIRTY){
            hourTime.add(startHour + ":"+startMinute+"-" + ++hour + ":00");
        }
        while (hour < endHour){
            hourTime.add(hour + ":00-" + hour + ":30");
            hourTime.add(hour + ":30-" + ++hour + ":00");
        }
        if (endMinute > 0 && endMinute < THIRTY){
            hourTime.add(hour + ":00-" + hour + ":" + endMinuteStr);
        }else if (endMinute >= THIRTY){
            hourTime.add(hour + ":00-" + hour + ":30");
            hourTime.add(hour + ":30-" + hour + ":" + endMinuteStr);
        }
        return hourTime;
    }


    @Override
    public void offline(Station station, String offlineReason, Long sysUserId) {
        // 添加下线处理记录
        Date now = new Date();
        OfflineHandleEvent offlineHandleEvent = new OfflineHandleEvent();
        offlineHandleEvent.setShopId(station.getShopId());
        offlineHandleEvent.setHandleId(station.getStationId());
        offlineHandleEvent.setHandleType(OfflineHandleEventType.STATION.getValue());
        offlineHandleEvent.setOfflineReason(offlineReason);
        offlineHandleEvent.setCreateTime(now);
        offlineHandleEvent.setHandlerId(sysUserId);
        offlineHandleEvent.setStatus(OfflineHandleEventStatus.OFFLINE_BY_PLATFORM.getValue());
        offlineHandleEvent.setUpdateTime(now);
        offlineHandleEventService.save(offlineHandleEvent);

        // 更新活动状态为下线
        stationMapper.updateStatus(station.getStationId(), StationEnum.OFFLINE.getValue());
    }

    @Override
    public void auditSeckill(OfflineHandleEventAuditParam offlineHandleEventAuditParam, Long sysUserId) {
        // 审核通过
        if (Objects.equals(offlineHandleEventAuditParam.getStatus(), OfflineHandleEventStatus.AGREE_BY_PLATFORM.getValue())) {
            // 更新自提点为关闭状态
            stationMapper.updateStatus(offlineHandleEventAuditParam.getHandleId(), StationEnum.INVALID.getValue());
        }
        // 审核不通过
        else if (Objects.equals(offlineHandleEventAuditParam.getStatus(), OfflineHandleEventStatus.DISAGREE_BY_PLATFORM.getValue())) {
            stationMapper.updateStatus(offlineHandleEventAuditParam.getHandleId(), StationEnum.AUDIT_FAIL.getValue());
        }
        // 更新审核时间
        offlineHandleEventService.auditOfflineEvent(offlineHandleEventAuditParam, sysUserId);
    }

    @Override
    @Cacheable(cacheNames = "station", key = "#stationId")
    public Station getStationInfoById(Long stationId) {
        Station stationInfoById = stationMapper.getStationInfoById(stationId);
        return stationInfoById;
    }

    @Override
    public Station getStationInfoByAccount(String account) {
        return stationMapper.getStationInfoByAccount(account);
    }

    @Override
    public List<StationSalesDto> getStationSalesRecord(Long shopId, Long stationId) {
        Date day = DateUtil.beginOfDay(new Date());
        Date month = DateUtil.beginOfMonth(day);
        List<StationSalesDto> stationSalesList = new ArrayList<>();
        StationSalesDto todayStationSalesDto = new StationSalesDto();
        todayStationSalesDto.setSalesType(1);
        StationSalesDto monthStationSalesDto = new StationSalesDto();
        monthStationSalesDto.setSalesType(2);
        double todayPayAmount = 0.0;
        int todayPayOrderNum = 0;
        // 当天支付总计人数
        int todayPayUserNum = 0;
        double monthPayAmount = 0.0;
        int monthPayOrderNum = 0;
        // 当月支付总人数
        int monthPayUserNum = 0;
        // 获取门店端自提的业绩
        List<StationSalesDto> stationSalesDb = stationMapper.getStationSalesRecord(shopId, stationId,day,month);
        Map<Integer, List<StationSalesDto>> stationSaleMap = stationSalesDb.stream().collect(Collectors.groupingBy(StationSalesDto::getSalesType));
        // 获取门店端核销的业绩
        List<StationSalesDto> stationSalesDtos = stationMapper.getStationWriteOffSalesRecord(shopId, stationId,day,month);
        Map<String, List<StationSalesDto>> writeOffSaleMap = stationSalesDtos.stream().collect(Collectors.groupingBy(StationSalesDto::getUserId));
        // 计算支付人数
        for (String userId : writeOffSaleMap.keySet()) {
            monthPayUserNum++;
            for (StationSalesDto stationSalesDto : writeOffSaleMap.get(userId)) {
                if(DateUtil.compare(stationSalesDto.getPayTime(),day) > 0) {
                    todayPayUserNum++;
                    break;
                }
            }
        }
        // 计算支付订单数和收入
        for (StationSalesDto stationSalesDto : stationSalesDtos) {
            // 核销收入计算为: （实付金额 + 平台优惠金额） * 核销数量 / 总数量
            double payAmount = Arith.add(stationSalesDto.getActualTotal(), stationSalesDto.getPlatformAmount());
            payAmount = Arith.div(Arith.mul(payAmount, stationSalesDto.getWriteOffNum()), stationSalesDto.getProductNums(),2);
            if(DateUtil.compare(stationSalesDto.getPayTime(),day) > 0) {
                todayPayAmount = Arith.add(todayPayAmount,payAmount);
                todayPayOrderNum ++;
            }
            monthPayAmount = Arith.add(monthPayAmount,payAmount);
            monthPayOrderNum++;
        }

        // 计算今日支付信息
        List<StationSalesDto> todayStationSalesDb = stationSaleMap.get(1);
        calculationAmountInfo(todayStationSalesDto, todayPayAmount, todayPayOrderNum, todayPayUserNum, writeOffSaleMap, todayStationSalesDb);
        // 计算本月支付信息
        List<StationSalesDto> monthStationSalesDb = stationSaleMap.get(2);
        calculationAmountInfo(monthStationSalesDto, monthPayAmount, monthPayOrderNum, monthPayUserNum, writeOffSaleMap, monthStationSalesDb);
        stationSalesList.add(todayStationSalesDto);
        stationSalesList.add(monthStationSalesDto);
        return stationSalesList;
    }

    /**
     * 计算业绩
     * @param stationSalesDto 返回给前端的业绩
     * @param payAmount 核销支付金额
     * @param payOrderNum 核销支付订单数
     * @param payUserNum 核销支付用户数
     * @param writeOffSaleMap 核销业绩数据
     * @param stationSalesDb 数据库的自提业绩
     */
    private void calculationAmountInfo(StationSalesDto stationSalesDto,  double payAmount, int payOrderNum,
                                       int payUserNum, Map<String, List<StationSalesDto>> writeOffSaleMap, List<StationSalesDto> stationSalesDb) {
        if(CollectionUtil.isEmpty(stationSalesDb)){
            stationSalesDto.setPayAmount(payAmount);
            stationSalesDto.setUserNum(payUserNum);
            stationSalesDto.setPayOrderNumber(payOrderNum);
            stationSalesDto.setCustomerUnitPrice(0.0);
            if (stationSalesDto.getUserNum() != 0) {
                stationSalesDto.setCustomerUnitPrice(Arith.div(stationSalesDto.getPayAmount(), stationSalesDto.getUserNum(), 2));
            }
        }else{
            payUserNum = stationSalesDb.size();
            for (StationSalesDto stationSales : stationSalesDb) {
                payAmount = Arith.add(payAmount,Arith.add(stationSales.getActualTotal(),stationSales.getPlatformAmount()));
                payOrderNum+= stationSales.getPayOrderNumber();
            }
            // 计算人数有无重复
            Map<String, StationSalesDto> salesDtoMap = stationSalesDb.stream().collect(Collectors.toMap(StationSalesDto::getUserId, stationSales -> stationSales));
            for (String userId : writeOffSaleMap.keySet()) {
                if(salesDtoMap.containsKey(userId)){
                    continue;
                }
                payUserNum++;
            }
            stationSalesDto.setPayAmount(payAmount);
            stationSalesDto.setUserNum(payUserNum);
            stationSalesDto.setPayOrderNumber(payOrderNum);
            stationSalesDto.setCustomerUnitPrice(0.0);
            if (stationSalesDto.getUserNum() != 0) {
                stationSalesDto.setCustomerUnitPrice(Arith.div(stationSalesDto.getPayAmount(), stationSalesDto.getUserNum(), 2));
            }
        }
    }

    @Override
    @CacheEvict(cacheNames = "station", key = "#station.stationId")
    public int insertStation(Station station) {
        Integer count = stationMapper.selectCount(new LambdaQueryWrapper<Station>()
                .eq(Station::getShopId, station.getShopId())
                .eq(Station::getStationName, station.getStationName()));
        if (count > 0) {
            throw new YamiShopBindException("自提点名称已存在");
        }
        Date date = new Date();
        station.setCreateTime(date);
        station.setUpdateTime(date);
        return stationMapper.insert(station);
    }

    @Override
    @CacheEvict(cacheNames = "station", key = "#station.stationId")
    public int updateStation(Station station) {
        Integer count = stationMapper.selectCount(new LambdaQueryWrapper<Station>()
                .eq(Station::getShopId, station.getShopId())
                .ne(Station::getStationId, station.getStationId())
                .eq(Station::getStationName, station.getStationName()));
        if (count > 0) {
            throw new YamiShopBindException("自提点名称已存在");
        }
        Station stationDb = stationMapper.selectById(station.getStationId());
        if (!Objects.equals(stationDb.getShopId(), station.getShopId())) {
            throw new YamiShopBindException("当前自提点不属于你的店铺");
        }
        if (!Objects.equals(stationDb.getStatus(), StationStatusEnum.CLOSURE.value()) && !Objects.equals(stationDb.getStatus(), StationStatusEnum.OPEN.value())) {
            // 数据库中自提点的状态不属于关闭或者营业状态时，商家不能直接修改自提点状态
            station.setStatus(stationDb.getStatus());
        }
        station.setUpdateTime(new Date());
        //自提点地址、电话改变，则更新tz_order_self_station表中对应的地址、电话
        String addr =null;
        Boolean addrChange = !stationDb.getProvinceId().equals(station.getProvinceId()) || !stationDb.getCityId().equals(station.getCityId()) ||
                !stationDb.getAreaId().equals(station.getAreaId()) || !StrUtil.equals(stationDb.getAddr(),station.getAddr());
        if(addrChange){
            addr = station.getProvince() + station.getCity() + station.getArea() + station.getAddr();
            stationMapper.changeStationAddr(station.getStationId(),addr, null);

        }
        String phone = null;
        Boolean phoneChange = !StrUtil.equals(stationDb.getPhone(),station.getPhone()) || !StrUtil.equals(stationDb.getPhonePrefix(),station.getPhonePrefix());
        if(phoneChange){
            phone = StrUtil.isNotBlank(station.getPhonePrefix())?station.getPhonePrefix() + "-":"";
            phone = phone + station.getPhone();
        }
        if (addrChange || phoneChange){
            stationMapper.changeStationAddr(station.getStationId(),addr, phone);
        }
        return stationMapper.updateById(station);
    }
    @Override
    @CacheEvict(cacheNames = "station", key = "#station.stationId")
    public void changeAccountInfo(Station station) {
        int count = stationMapper.getCountByAccount(station.getAccount().trim(),station.getStationId());
        if (count > 0){
            // 此账号已存在，请输入新的账号
            throw new YamiShopBindException("yami.station.account.exist");
        }
        Station stationNew = new Station();
        stationNew.setStationId(station.getStationId());
        stationNew.setAccount(station.getAccount());
        stationNew.setPassword(station.getPassword());
        stationMapper.updateById(stationNew);
    }

    @Override
    public int getCountByAccount(String account, Long stationId) {
        return stationMapper.getCountByAccount(account.trim(),stationId);
    }

    @Override
    @CacheEvict(cacheNames = "station", key = "#stationId")
    public int removeStationById(Long stationId) {
        return stationMapper.deleteById(stationId);
    }

    @Override
    @CacheEvict(cacheNames = "station", key = "#stationId")
    public void removeCacheByStationId(Long stationId) {

    }
}
